package aps.module_Fusion;

//open gl
import net.minecraft.src.Block;
import net.minecraft.src.Item;
import net.minecraft.src.ItemStack;
import net.minecraft.src.ModLoader;
import net.minecraft.src.RenderBlocks;
import net.minecraft.src.Tessellator;
import net.minecraftforge.client.MinecraftForgeClient;
import net.minecraftforge.common.Configuration;
import net.minecraftforge.liquids.LiquidContainerRegistry;
import net.minecraftforge.liquids.LiquidDictionary;
import net.minecraftforge.liquids.LiquidStack;

import org.lwjgl.opengl.GL11;

import aps.BuildcraftAPS;
import aps.module;
import aps.module_Core.module_Core;
import aps.module_Core.module_Core.APSBlockTypes;
import aps.module_Fusion.proxy.APSFusionProxy;
import buildcraft.BuildCraftCore;
import buildcraft.BuildCraftEnergy;
import buildcraft.BuildCraftFactory;
import buildcraft.BuildCraftTransport;
import buildcraft.api.gates.ActionManager;
import buildcraft.api.gates.Trigger;
import buildcraft.api.recipes.AssemblyRecipe;
import buildcraft.api.recipes.RefineryRecipe;
import buildcraft.core.proxy.CoreProxy;
import buildcraft.core.utils.Utils;
import buildcraft.transport.BlockGenericPipe;
import buildcraft.transport.ItemPipe;
import buildcraft.transport.Pipe;
import buildcraft.transport.TransportProxyClient;
//forge
//aps
//buildcraft
//import buildcraft.core.ItemBuildCraftTexture;
//vanilla minecraft

public class module_Fusion extends module
{
	private int TokamakID = 0;
	
	int ItemIdTokamakPlating;
	int ItemIdTokamakChamber;
	int ItemIdTokamakControl;
	int ItemIdTokamakExtractor;
	int ItemIdTokamakInput;
	int ItemIdTokamakOutput;
	int ItemIdPipeLapis;
	int ItemIdHeavyWaterBucket;
	int ItemIdHeavyWater;
	int ItemIdEnergyReader;
	
	int ItemIDPipe;
	
	public static int TextureIdPipePowerLapis;
	
	public static Item tokamakPlatingItem = null;
	public static Item tokamakChamberItem = null;
	public static Item tokamakControlItem = null;
	public static Item tokamakExtractorItem = null;
	public static Item tokamakInputItem = null;
	public static Item tokamakOutputItem = null;
	
	public static Item heavyWaterBucketItem = null;
	public static Item heavyWater = null;
	
	public static ItemPipe advancedPowerOutput = null;
	
	//public static ItemPipe pipePowerLapis = null;
	
	public static Item energyReader = null;
	
	public static Trigger tokamakIdlingTrigger;
	public static Trigger tokamakTempReachedTrigger;
	public static Trigger tokamakOutputQuaterTrigger;
	public static Trigger tokamakOutputHalfTrigger;
	public static Trigger tokamakOutput3QuatersTrigger;
	public static Trigger tokamakOutputMaxTrigger;
	
	public static Trigger pipeCloggedTrigger;
	public static Trigger pipeFreeTrigger;
	
	public static int MaxTemp;
	public static int CoolRate;
	public static float FusionFraction;
	
	public static GuiEnergyReader guiEnergyReader;
	
	public module_Fusion() {}
	
	@Override
	public void preInit()
	{
		//config
		MaxTemp = Integer.parseInt(BuildcraftAPS.Conf.get("Fusion", "MaxTemperature", 10000000).value);
		CoolRate = Integer.parseInt(BuildcraftAPS.Conf.get("Fusion", "CoolRate", 1000).value);
		FusionFraction = (float) Integer.parseInt(BuildcraftAPS.Conf.get("Fusion", "TokamakFusionTemperaturePercentage", 80).value) / (float) 100;
		
		ItemIdTokamakPlating = Integer.parseInt(BuildcraftAPS.Conf.get(Configuration.CATEGORY_ITEM, "TokamakPlating.ID", BuildcraftAPS.defItemStartID + 2).value);
		ItemIdTokamakChamber = Integer.parseInt(BuildcraftAPS.Conf.get(Configuration.CATEGORY_ITEM, "TokamakChamber.ID", BuildcraftAPS.defItemStartID + 2 + 1).value);
		ItemIdTokamakControl = Integer.parseInt(BuildcraftAPS.Conf.get(Configuration.CATEGORY_ITEM, "TokamakControl.ID", BuildcraftAPS.defItemStartID + 2 + 2).value);
		ItemIdTokamakExtractor = Integer.parseInt(BuildcraftAPS.Conf.get(Configuration.CATEGORY_ITEM, "TokamakExtractor.ID", BuildcraftAPS.defItemStartID + 2 + 3).value);
		ItemIdTokamakInput = Integer.parseInt(BuildcraftAPS.Conf.get(Configuration.CATEGORY_ITEM, "TokamakInput.ID", BuildcraftAPS.defItemStartID + 2 + 4).value);
		ItemIdTokamakOutput = Integer.parseInt(BuildcraftAPS.Conf.get(Configuration.CATEGORY_ITEM, "TokamakOutput.ID", BuildcraftAPS.defItemStartID + 2 + 5).value);
		ItemIDPipe = Integer.parseInt(BuildcraftAPS.Conf.get(Configuration.CATEGORY_ITEM, "AdvancedOutputPipeGrade1.ID", BuildcraftAPS.defItemStartID + 2 + 6).value);
		ItemIdHeavyWaterBucket = Integer.parseInt(BuildcraftAPS.Conf.get(Configuration.CATEGORY_ITEM, "HeavyWaterBucket.ID", BuildcraftAPS.defItemStartID + 2 + 7).value);
		ItemIdHeavyWater = Integer.parseInt(BuildcraftAPS.Conf.get(Configuration.CATEGORY_ITEM, "HeavyWater.ID", BuildcraftAPS.defItemStartID + 2 + 8).value);
		ItemIdEnergyReader = Integer.parseInt(BuildcraftAPS.Conf.get(Configuration.CATEGORY_ITEM, "EnergyReader.ID", BuildcraftAPS.defItemStartID + 2 + 9).value);
		
		advancedPowerOutput = createPipe(ItemIDPipe, APSPipePower.class, "Advanced Output Pipe");
	}
	
	@Override
	public void initialize()
	{
		tokamakPlatingItem = (new ItemTokamakPlating(ItemIdTokamakPlating)).setItemName("tokamakplatingitem");
		tokamakChamberItem = (new ItemTokamakChamber(ItemIdTokamakChamber)).setItemName("tokamakchamberitem");
		tokamakControlItem = (new ItemTokamakControl(ItemIdTokamakControl)).setItemName("tokamakcontrolitem");
		tokamakExtractorItem = (new ItemTokamakExtractor(ItemIdTokamakExtractor)).setItemName("tokamakextractoritem");
		tokamakInputItem = (new ItemTokamakInput(ItemIdTokamakInput)).setItemName("tokamakinputitem");
		tokamakOutputItem = (new ItemTokamakOutput(ItemIdTokamakOutput)).setItemName("tokamakoutputitem");
		
		heavyWaterBucketItem = (new ItemHeavyWaterBucket(ItemIdHeavyWaterBucket)).setItemName("heavywaterbucket").setContainerItem(Item.bucketEmpty);
		heavyWater = (new ItemHeavyWater(ItemIdHeavyWater)).setItemName("heavywater");
		
		energyReader = (new ItemEnergyReader(ItemIdEnergyReader)).setItemName("energyreader");
		
		tokamakIdlingTrigger = new TriggerTokamak(99, TriggerTokamak.TriggerType.TokamakIdling);
		tokamakTempReachedTrigger = new TriggerTokamak(100, TriggerTokamak.TriggerType.TokamakTempReached);
		tokamakOutputQuaterTrigger = new TriggerTokamak(101, TriggerTokamak.TriggerType.TokamakOutputQuater);
		tokamakOutputHalfTrigger = new TriggerTokamak(102, TriggerTokamak.TriggerType.TokamakOutputHalf);
		tokamakOutput3QuatersTrigger = new TriggerTokamak(103, TriggerTokamak.TriggerType.TokamakOutput3Quaters);
		tokamakOutputMaxTrigger = new TriggerTokamak(104, TriggerTokamak.TriggerType.TokamakOutputMax);
		
		pipeCloggedTrigger = new TriggerAdvancedPipe(105, TriggerAdvancedPipe.TriggerType.pipeClogged);
		pipeFreeTrigger = new TriggerAdvancedPipe(106, TriggerAdvancedPipe.TriggerType.pipeFree);
		
		ActionManager.registerTriggerProvider(new APSFusionTriggerProvider());
		
		//####################Tokamak Plating
		ModLoader.addName(tokamakPlatingItem, "Tokamak Plating");
    	ModLoader.addRecipe(new ItemStack(tokamakPlatingItem), new Object[]{ "DIO", "DIO", "DIO",
    															Character.valueOf('D'), Item.diamond,
    															Character.valueOf('I'), Item.ingotIron,
    															Character.valueOf('O'), Block.obsidian
    															});
    	
    	//####################Tokamak Chamber
    	ModLoader.addName(tokamakChamberItem, "Tokamak Chamber");
    	ModLoader.addRecipe(new ItemStack(tokamakChamberItem), new Object[]{ "IPI", "P P", "IPI",
    															Character.valueOf('I'), Item.ingotIron,
    															Character.valueOf('P'), tokamakPlatingItem
    															});
    	
    	//####################Control Module
    	ModLoader.addName(tokamakControlItem, "Control Module");
    	ModLoader.addRecipe(new ItemStack(tokamakControlItem), new Object[]{ "IrI", "rCr", "IrI",
    	                                                        Character.valueOf('I'), Item.ingotIron,
    	                                                        Character.valueOf('C'), new ItemStack(module_Core.APSPowerCoreItem, 1, 3),
    	                                                        Character.valueOf('r'), Item.redstoneRepeater
    	                                                        });
    	
    	//####################Deuterium Extractor
    	ModLoader.addName(tokamakExtractorItem, "Deuterium Extractor");
    	ModLoader.addRecipe(new ItemStack(tokamakExtractorItem), new Object[]{ "ClC", "ere", "clc",
    															Character.valueOf('C'), new ItemStack(module_Core.APSPowerCoreItem, 1, 1),
    															Character.valueOf('l'), BuildCraftTransport.pipeLiquidsGold,
    															Character.valueOf('e'), new ItemStack (BuildCraftEnergy.engineBlock, 1, 2),
    															Character.valueOf('r'), BuildCraftFactory.refineryBlock,
    															Character.valueOf('c'), BuildCraftTransport.pipePowerGold
    															});
    	
    	//####################Input Module
    	ModLoader.addName(tokamakInputItem, "Input Module");
    	ModLoader.addRecipe(new ItemStack(tokamakInputItem), new Object[]{ "DcD", "Rcl", "Rcl",
    															Character.valueOf('D'), Item.diamond,
    															Character.valueOf('c'), BuildCraftTransport.pipePowerGold,
    															Character.valueOf('R'), Item.redstone,
    															Character.valueOf('l'), BuildCraftTransport.pipeLiquidsGold
    															});
    	
    	//####################Output Module
    	ModLoader.addName(tokamakOutputItem, "Output Module");
    	ModLoader.addRecipe(new ItemStack(tokamakOutputItem), new Object[]{ "Rcd", "Rcd", "DwD",
    															Character.valueOf('R'), Item.redstone,
    															Character.valueOf('c'), BuildCraftTransport.pipePowerGold,
    															Character.valueOf('d'), BuildCraftCore.diamondGearItem,
    															Character.valueOf('w'), BuildCraftTransport.pipePowerWood,
    															Character.valueOf('D'), Item.diamond
    															});
    	
    	//####################Heavy Water Bucket
    	ModLoader.addName(heavyWaterBucketItem, "Heavy Water Bucket");
    	
    	//####################Heavy Water
    	ModLoader.addName(heavyWater, "Heavy Water");
    	LiquidDictionary.getOrCreateLiquid("Heavy Water", new LiquidStack(heavyWater.shiftedIndex, 0)); //updated to LiquidManager API - Seph
    	//RefineryRecipe.registerRefineryRecipe(new RefineryRecipe(Block.waterStill.blockID, 5, 0, 0, 20, heavyWater.shiftedIndex, 1, 1));
    	RefineryRecipe.registerRefineryRecipe(new RefineryRecipe(new LiquidStack(Block.waterStill, 2), new LiquidStack(Block.waterStill, 2), new LiquidStack(heavyWater.shiftedIndex, 4), 5, 1));
    	//ModLoader.getMinecraftInstance().renderEngine.registerTextureFX(new TextureHeavyWaterFX());
    	APSFusionProxy.proxy.registerTextureFX(); //registering the texture fx is done here.
		
    	
    	
    	
		//#################### Power Pipes
		TextureIdPipePowerLapis = CoreProxy.proxy.addCustomTexture(BuildcraftAPS.imageFilesRoot + "PipePowerLapis.png"); //update to new CoreProxy -Seph
    	
		
		// Changed recipes to use the assembly table
		
		AssemblyRecipe.assemblyRecipes.add(new AssemblyRecipe(new ItemStack[] {new ItemStack(module_Core.APSPowerCoreItem,1,2), new ItemStack(BuildCraftTransport.pipePowerGold,1)},
				1000,
				new ItemStack(advancedPowerOutput)
				));
		
		
    	/*ModLoader.addRecipe(new ItemStack(advancedPowerOutput1), new Object[]{ "AB",
																Character.valueOf('A'), new ItemStack(module_Core.APSPowerCoreItem, 1, 0),
																Character.valueOf('B'), BuildCraftTransport.pipePowerGold
																});
    	ModLoader.addRecipe(new ItemStack(advancedPowerOutput2), new Object[]{ "AB",
																Character.valueOf('A'), new ItemStack(module_Core.APSPowerCoreItem, 1, 1),
																Character.valueOf('B'), BuildCraftTransport.pipePowerGold
																});
    	ModLoader.addRecipe(new ItemStack(advancedPowerOutput3), new Object[]{ "AB",
																Character.valueOf('A'), new ItemStack(module_Core.APSPowerCoreItem, 1, 2),
																Character.valueOf('B'), BuildCraftTransport.pipePowerGold
																});
    	ModLoader.addRecipe(new ItemStack(advancedPowerOutput4), new Object[]{ "AB",
																Character.valueOf('A'), new ItemStack(module_Core.APSPowerCoreItem, 1, 3),
																Character.valueOf('B'), BuildCraftTransport.pipePowerGold
																});*/
    	
    	
    	
		
		//####################Tokamak Generator Kit
    	ModLoader.addName(new ItemStack(module_Core.APSKitItem, 1, TokamakID + ItemEnergy), "Fusion Generator Kit");
    	ModLoader.addRecipe(new ItemStack(module_Core.APSKitItem, 1, TokamakID + ItemEnergy), new Object[]{ "oOo", "CTD", "oIo",
                                                                Character.valueOf('o'), Block.obsidian,
                                                                Character.valueOf('O'), tokamakOutputItem,
                                                                Character.valueOf('C'), tokamakControlItem,
                                                                Character.valueOf('T'), tokamakChamberItem,
                                                                Character.valueOf('D'), tokamakExtractorItem,
                                                                Character.valueOf('I'), tokamakInputItem
                                                                });

    	//####################Tokamak Generator
    	module_Core.AddMetaSubblock(TokamakID, TileEntityTokamakGenerator.class, "tokamakgenerator", "Fusion Generator", APSBlockTypes.Energy, true, false);
    	AssemblyRecipe.assemblyRecipes.add(new AssemblyRecipe(new ItemStack[] {
                                                                new ItemStack(module_Core.APSKitItem, 1, TokamakID + ItemEnergy)},
                                                                15000, 
                                                                new ItemStack(module_Core.APSMetaBlockEnergy, 1, TokamakID)
                                                                ));
    	
    	//#################### Energy Reader
    	ModLoader.addName(energyReader, "Energy Reader [WIP]");
    	//ModLoader.addRecipe(new ItemStack(energyReader), new Object[] 	{ "bbb", "   ", "   ",
    																	//	Character.valueOf('b'), Block.dirt
    																	//});
	}
	
	@Override
	public void clientInit()
	{
		//Pipe renderer
		//MinecraftForgeClient.registerItemRenderer(pipePowerLapis.shiftedIndex, TransportProxyClient.pipeItemRenderer);
		MinecraftForgeClient.registerItemRenderer(advancedPowerOutput.shiftedIndex, TransportProxyClient.pipeItemRenderer);
		MinecraftForgeClient.preloadTexture(BuildcraftAPS.imageFilesRoot + "PipePowerLapis.png");
		
		//Energy reader gui
		guiEnergyReader = new GuiEnergyReader();
	}
	

	private static ItemPipe createPipe (int defaultID, Class <? extends Pipe> clas, String descr) {
		String name = clas.getSimpleName();
		
		ItemPipe res =  BlockGenericPipe.registerPipe(defaultID, clas);
		res.setItemName(name);
		//res.setItemName("Pipe Power Lapis");
		
		//res.setItemName(descr);
		CoreProxy.proxy.addName(res, descr);

		return res;
	}
	
	public void renderInventory(RenderBlocks renderblocks, int itemID,int meta) {
		Tessellator tessellator = Tessellator.instance;

		Block block = BuildCraftTransport.genericPipeBlock;
		int textureID = ((ItemPipe) Item.itemsList [itemID]).getTextureIndex();
		
		block.setBlockBounds(Utils.pipeMinPos, 0.0F, Utils.pipeMinPos,
				Utils.pipeMaxPos, 1.0F, Utils.pipeMaxPos);
		block.setBlockBoundsForItemRender();
		GL11.glTranslatef(-0.5F, -0.5F, -0.5F);
		tessellator.startDrawingQuads();
		tessellator.setNormal(0.0F, -1F, 0.0F);
		renderblocks.renderBottomFace(block, 0.0D, 0.0D, 0.0D, textureID);
		tessellator.draw();
		tessellator.startDrawingQuads();
		tessellator.setNormal(0.0F, 1.0F, 0.0F);
		renderblocks.renderTopFace(block, 0.0D, 0.0D, 0.0D, textureID);
		tessellator.draw();
		tessellator.startDrawingQuads();
		tessellator.setNormal(0.0F, 0.0F, -1F);
		renderblocks.renderEastFace(block, 0.0D, 0.0D, 0.0D, textureID);
		tessellator.draw();
		tessellator.startDrawingQuads();
		tessellator.setNormal(0.0F, 0.0F, 1.0F);
		renderblocks.renderWestFace(block, 0.0D, 0.0D, 0.0D, textureID);
		tessellator.draw();
		tessellator.startDrawingQuads();
		tessellator.setNormal(-1F, 0.0F, 0.0F);
		renderblocks.renderNorthFace(block, 0.0D, 0.0D, 0.0D, textureID);
		tessellator.draw();
		tessellator.startDrawingQuads();
		tessellator.setNormal(1.0F, 0.0F, 0.0F);
		renderblocks.renderSouthFace(block, 0.0D, 0.0D, 0.0D, textureID);
		tessellator.draw();
		GL11.glTranslatef(0.5F, 0.5F, 0.5F);
		block.setBlockBounds(0.0F, 0.0F, 0.0F, 1.0F, 1.0F, 1.0F);
		
	}
	
	public void postInit() {}
}
